import QQMapWX from "@/libs/qqmap-wx-jssdk.min"; //改源码 export default QQMapWX
let MapSdk;

const state = {
    openid: '',
    userInfo: null,
    isGetUserInfo: false, //用户是否上传了用户信息
    gpsLocation: '',
    location: ''
}

const mutations = {
    SET_GPS_CITY(state, city) {
        state.location = state.gpsLocation = city
    },
    SET_USER_INFO(state, userInfo) {
        state.userInfo = userInfo
        state.isGetUserInfo = true
    },
    SET_LOGIN_INFO(state, info) {
        const { code, openid } = info
        state.openid = openid
        state.isGetUserInfo = code === 0 ? true : false
    }
}

const actions = {
    async login({ commit }) {
        // const code = await this.$native.login()
        // const { retCode, wxOpenId } = await this.$http.login({ code })
        // 0:用户已经存在于数据库中，并已经设置用户信息
        // 101: 用户已经存在于数据库中，但未设置用户信息,这里需要小程序切换到提醒用户补充个人信息的页面。
        const retCode = 101; //101
        const wxOpenId = 'oBdny5INTntetmhY-dpRXYd26_k0';
        commit('SET_LOGIN_INFO', { code: retCode, openid: wxOpenId })
    },
    // 获取GPS定位所在城市
    getLocationCity({ commit }, location) {
        // location = { latitude: 42.638000, longitude: 125.711060 } 测试数据
        MapSdk = new QQMapWX({
            key: "VFCBZ-4ZP6U-ZS5VO-BORMM-EFHA3-NCFK6"
        });
        return new Promise(resolve => {
            MapSdk.reverseGeocoder({
                location: `${location.latitude},${location.longitude}`,
                success(res) {
                    commit('SET_GPS_CITY', res.result.ad_info.city.replace('市', ''))
                }
            });
        });
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}